import Vapor

struct ReferenceTextController: RouteCollection {

  func boot(routes: RoutesBuilder) throws {
    let routes = routes.grouped("api", "referencetext")
//    routes.get(use: getAllHandler)
    routes.post(use: createHandler)
//    routes.get(":simplebuildingID", use: getHandler)
    routes.put(use: updateHandler)
//    routes.delete(":id", use: deleteHandler)
//    routes.get("search", use: searchHandler)

  }

    func getAllHandler(_ req: Request) throws -> EventLoopFuture<[ReferenceText]> {
        ReferenceText.query(on: req.db).all()
    }
    
    func createHandler(_ req: Request) throws -> EventLoopFuture<ReferenceText> {
      let data = try req.content.decode(ReferenceTextData.self)
        let text = ReferenceText(id: data.id, fullText: data.fullText ?? "", isTestimonial: data.isTestimonial, languageIsoCode: data.languageIsoCode ?? "", referenceID: data.referenceID)
      return text.save(on: req.db).map { text }
    }

//    func getHandler(_ req: Request) throws -> EventLoopFuture<SimpleBuilding> {
//        SimpleBuilding.find(req.parameters.get("simplebuildingID"), on: req.db)
//      .unwrap(or: Abort(.notFound))
//    }

    func updateHandler(_ req: Request) throws -> EventLoopFuture<ReferenceText> {
      let updateData = try req.content.decode(ReferenceTextData.self)
        return ReferenceText.find(updateData.id, on: req.db)
        .unwrap(or: Abort(.notFound)).flatMap { text in
            text.id = updateData.id
            text.fullText = updateData.fullText
            text.isTestimonial = updateData.isTestimonial
            text.languageIsoCode = updateData.languageIsoCode
            text.$reference.id = updateData.referenceID
          return text.save(on: req.db).map {
            text
          }
      }
    }

//    func deleteHandler(_ req: Request)
//      throws -> EventLoopFuture<HTTPStatus> {
//        SimpleBuilding.find(req.parameters.get("simplebuildingID"), on: req.db)
//        .unwrap(or: Abort(.notFound))
//        .flatMap { acronym in
//          acronym.delete(on: req.db)
//            .transform(to: .noContent)
//      }
//    }

//    func searchHandler(_ req: Request) throws -> EventLoopFuture<[SimpleBuilding]> {
//      guard let searchTerm = req
//        .query[String.self, at: "term"] else {
//        throw Abort(.badRequest)
//      }
//        return SimpleBuilding.query(on: req.db).group(.or) { or in
//            or.filter(\.$name == searchTerm)
//            or.filter(\.$name == searchTerm)
//
//
//        }.all()
//    }

//    func getFirstHandler(_ req: Request) throws -> EventLoopFuture<SimpleBuilding> {
//      return SimpleBuilding.query(on: req.db)
//        .first()
//        .unwrap(or: Abort(.notFound))
//    }
//
//    func sortedHandler(_ req: Request) throws -> EventLoopFuture<[SimpleBuilding]> {
//      return SimpleBuilding.query(on: req.db).sort(\.$name, .ascending).all()
//    }
//    
//    func getProjectHandler(_ req: Request) throws -> EventLoopFuture<Project> {
//        SimpleBuilding.find(req.parameters.get("simplebuildingID"), on: req.db)
//      .unwrap(or: Abort(.notFound))
//      .flatMap { simpleBuilding in
//        simpleBuilding.$project.get(on: req.db)
//      }
//    }
}

struct ReferenceTextData: Content {
    var id:UUID
    
    var fullText: String?
    
    var isTestimonial: Bool
    
    var languageIsoCode: String?
    
    var referenceID: UUID
}
